package com.system.vote.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.vote.admin.entity.TVote;
import com.system.vote.admin.entity.TVoteItem;
import com.system.vote.admin.entity.TWxUserinfo;
import com.system.vote.admin.service.TVoteItemService;
import com.system.vote.admin.service.TVoteService;
import com.system.vote.admin.service.TWxUserinfoService;
import com.system.vote.common.utils.StringUtils;
import com.system.vote.core.customBean.PageBean;
import com.system.vote.core.http.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(tags = "PC后台投票信息列表管理")
@RequestMapping("/vote/voteInfo")
@Slf4j
public class VoteInfoController {

    @Autowired
    private TVoteService tVoteService;

    @Autowired
    private TVoteItemService tVoteItemService;
    @Autowired
    private TWxUserinfoService tWxUserinfoService;


    @PostMapping("/list")
    @PreAuthorize("hasAuthority('vote:voteInfo:list')")
    public Result list(@RequestBody PageBean pageBean) {
        String query = pageBean.getQuery();
        String type = pageBean.getType();
        String isOver = pageBean.getIsOver();

        QueryWrapper<TVote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(type), "type", type);
        queryWrapper.like(StringUtils.isNotEmpty(query), "title", query);
        if(StringUtils.isNotEmpty(isOver)){
            if(Objects.equals(isOver,"1")){
                queryWrapper.ge("vote_end_time",new Date());
            }
            else{
                queryWrapper.lt("vote_end_time",new Date());
            }
        }
        queryWrapper.orderByDesc("vote_end_time");
        Page<TVote> pageResult = tVoteService.page(
                new Page<>(pageBean.getPageNum(),
                        pageBean.getPageSize()),
                queryWrapper);

        List<TVote> listData = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", listData);
        resultMap.put("total", pageResult.getTotal());
        return Result.success(resultMap);
    }

    @PostMapping("/updateVoteStatus")
    @PreAuthorize("hasAuthority('vote:voteInfo:edit')")
    public Result updateVoteStatus(@RequestBody TVote tVote) {
        TVote cuttentTVote = tVoteService.getById(tVote.getId());
        cuttentTVote.setStatus(tVote.getStatus());
        tVoteService.updateById(tVote);
        String msg = "";
        if (Objects.equals(tVote.getStatus(), "0")) {
            msg = "启用投票信息成功";
        } else {
            msg = "禁用投票信息成功";
        }
        return Result.success(msg);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('vote:voteInfo:query')")
    public Result findById(@PathVariable Integer id) {
        TVote tVote = tVoteService.getById(id);
        TWxUserinfo tWxUserinfo = tWxUserinfoService.getOne(new QueryWrapper<TWxUserinfo>().eq("id", tVote.getUserid()));
        tVote.setTWxUserinfo(tWxUserinfo);
        List<TVoteItem> items = tVoteItemService.list(new QueryWrapper<TVoteItem>().eq("vote_id", tVote.getId()));
        tVote.setItems(items);
        return Result.success(tVote);
    }
}
